﻿using Tszy.Unlimited.Base.Extensions;
namespace Tszy.Examples.Https;

internal class HttpService
{
    public static void Execute()
    {
        //HttpGet();
        //HttpPost();
        //HttpPostJson();
        //HttpPostHeader();
        HttpPostFile();
    }

    private static void HttpGet()
    {
        var url = "http://localhost:2003/api/test2";
        var result = url.HttpGet(new { VID = "1" })
            .ExecuteAsync().ConfigureAwait(false).GetAwaiter().GetResult();

        var result2 = url.HttpGet("/api/test2").SetParameters(new { VID = "1", Secretkey = "TEST" }).ExecuteAsync<HttpPost2Result>().ConfigureAwait(false).GetAwaiter().GetResult();
    }

    private static void HttpPost()
    {
        var url = "http://localhost:2003";
        var result = url
            .HttpPost("/api/test3")
            .SetParameters(new
            {
                VID = "1"
            })
            .ExecuteAsync().ConfigureAwait(false).GetAwaiter().GetResult();

        var result2 = url.HttpPost("/api/test3").SetParameters(new { VID = "1", Secretkey = "TEST" }).ExecuteAsync<HttpPost2Result>().ConfigureAwait(false).GetAwaiter().GetResult();
    }

    public class HttpPost2Result
    {
        public string VID { get; set; } = string.Empty;
        public string Secretkey { get; set; } = string.Empty;
        public int Scene { get; set; }
        public string Server { get; set; } = string.Empty;
        public string Token { get; set; } = string.Empty;
        public string Ip { get; set; } = string.Empty;
    }

    private static void HttpPostJson()
    {
        var url = "https://0.vaptcha.com";
        var data = new
        {
            id = "62abdcfabe382d373f6ab9ed",
            secretkey = "2df45025172d4328a84b700a9d2c2897",
            scene = 3,
            token = "1655706981Km0HKAb3bfc",
            ip = "116.11.47.118"
        };

        var result = url
            .HttpPostJosn("/verify")
            .SetParameters(data)
            .ExecuteAsync().ConfigureAwait(false).GetAwaiter().GetResult();

        var result2 = url.HttpPostJosn("/verify").SetParameters(data).ExecuteAsync<HttpPostJson3Result>().ConfigureAwait(false).GetAwaiter().GetResult();
    }

    public class HttpPostJson3Result
    {
        public string msg { get; set; } = string.Empty;
        public int success { get; set; }
    }

    private static void HttpPostHeader()
    {
        "http://localhost:8084".HttpPostJosn("added/service/order/upload/image")
            .WithHeader("Sign", "123")
            .WithHeader("VersionCode", "1")
            .WithHeader("fromApp", "ios")
            .WithHeader("channel", "test")
            .SetParameters(new { taskOrderId = 50000001 })
            .ExecuteAsync().ConfigureAwait(false).GetAwaiter().GetResult();
    }

    private static async void HttpPostFile()
    {
        var api = "https://asappapi.zhimicn.com/".CombineUrl("added/service/order/upload/image");
        var taskOrderId = 50000001;
        //var result = api
        //    .HttpPost(new { taskOrderId })
        //    .AddFormParameter("test1", "1")
        //    .AddFile("upload_image", "C:\\Users\\10909\\Pictures\\1.jpg")
        //    .ExecuteAsync<ApiResult<TaskOrderUploadImageResult>>().ConfigureAwait(false).GetAwaiter().GetResult();

        //var res = await (new Url(api)).PostMultipartAsync(mp =>
        //{
        //    mp.AddFile("upload_image", "C:\\Users\\10909\\Pictures\\1.jpg");
        //    //mp.AddString("taskOrderId", $"{taskOrderId}");
        //    mp.AddStringParts(new { taskOrderId });
        //});

        //var r = await res.GetStringAsync();
        var result = await api
            .HttpPost(new { taskOrderId })
            .AddFile("upload_image", "C:\\Users\\10909\\Pictures\\1.jpg")
            .ExecuteAsync<ApiResult<TaskOrderUploadImageResult>>();
    }
}

public class ApiResult
{
    private string _errorCode = string.Empty;
    private string _errorMessage = string.Empty;
    private object _data = default;

    public ApiResult()
    {
    }

    public ApiResult(bool status, string code, string msg, object data)
    {
        this.Status = status;
        this.ErrorCode = code;
        this.ErrorMessage = msg;
        this.Data = data;
    }

    /// <summary>
    /// 状态
    /// </summary>
    public bool Status { get; set; }

    /// <summary>
    /// 返回码
    /// </summary>
    public string ErrorCode
    {
        get
        {
            if (string.IsNullOrWhiteSpace(_errorCode))
            {
                return string.Empty;
            }
            return _errorCode.Trim();
        }
        set
        {
            _errorCode = value;
        }
    }

    /// <summary>
    /// 信息
    /// </summary>
    public string ErrorMessage
    {
        get
        {
            if (string.IsNullOrWhiteSpace(_errorMessage))
            {
                return string.Empty;
            }
            return _errorMessage.Trim();
        }
        set
        {
            _errorMessage = value;
        }
    }

    /// <summary>
    /// 数据
    /// </summary>
    public object Data
    {
        get
        {
            if (_data == null)
            {
                return default;
            }
            return _data;
        }
        set
        {
            _data = value;
        }
    }

    #region 方法

    public static ApiResult Result(bool status, string code, string msg)
    {
        return new ApiResult(status, code, msg, null);
    }

    public static ApiResult Success(string msg = "")
    {
        return new ApiResult(true, ApiCodeModel.Success, msg, null);
    }

    public static ApiResult Error(string msg = "")
    {
        return Error(ApiCodeModel.Error, msg);
    }

    public static ApiResult Error(string code, string msg = "")
    {
        return new ApiResult(false, code, msg, null);
    }

    #endregion 方法
}

public class ApiResult<T> : ApiResult
{
    #region 初始化

    private string _errorCode = string.Empty;
    private string _errorMessage = string.Empty;
    private T _data = default;

    public ApiResult()
    {
    }

    public ApiResult(bool status, string code, string msg, T data)
    {
        this.Status = status;
        this.ErrorCode = code;
        this.ErrorMessage = msg;
        this.Data = data;
    }

    /// <summary>
    /// 状态
    /// </summary>
    public new bool Status { get; set; }

    /// <summary>
    /// 返回码
    /// </summary>
    public new string ErrorCode
    {
        get
        {
            if (string.IsNullOrWhiteSpace(_errorCode))
            {
                return string.Empty;
            }
            return _errorCode.Trim();
        }
        set
        {
            _errorCode = value;
        }
    }

    /// <summary>
    /// 信息
    /// </summary>
    public new string ErrorMessage
    {
        get
        {
            if (string.IsNullOrWhiteSpace(_errorMessage))
            {
                return string.Empty;
            }
            return _errorMessage.Trim();
        }
        set
        {
            _errorMessage = value;
        }
    }

    /// <summary>
    /// 数据
    /// </summary>
    public new T Data
    {
        get
        {
            if (_data == null)
            {
                return default;
            }
            return _data;
        }
        set
        {
            _data = value;
        }
    }

    #endregion 初始化

    #region 方法

    public static ApiResult<T> Result(bool status, string code = "", string msg = "", T data = default)
    {
        return new ApiResult<T>(status, code, msg, data);
    }

    public static ApiResult<T> Success(T data = default, string msg = "")
    {
        data ??= CreateInstance<T>();

        return new ApiResult<T>(true, ApiCodeModel.Success, msg, data);
    }

    public new static ApiResult<T> Error(string msg = "")
    {
        return Error(ApiCodeModel.Error, msg);
    }

    public new static ApiResult<T> Error(string code, string msg = "")
    {
        return new ApiResult<T>(false, code, msg, CreateInstance<T>());
    }

    private static TE CreateInstance<TE>()
    {
        try
        {
            return Activator.CreateInstance<TE>();
        }
        catch
        {
            return default;
        }
    }

    #endregion 方法
}

/// <summary>
/// 接口错误码配置
/// </summary>
public static class ApiCodeModel
{
    /// <summary>
    /// 请求成功
    /// </summary>
    public const string Success = "200";

    /// <summary>
    /// 无访问权
    /// </summary>
    public const string Unauthorized = "401";

    /// <summary>
    /// 权限不足
    /// </summary>
    public const string Forbidden = "403";

    /// <summary>
    /// 系统异常
    /// </summary>
    public const string Exception = "500";

    /// <summary>
    /// 默认错误
    /// </summary>
    public const string Error = "0";

    /// <summary>
    /// 请求参数错误
    /// </summary>
    public const string ErrorVerify = "1000";

    #region 拓展方法

    /// <summary>
    /// 请求成功？
    /// </summary>
    public static bool IsOk<T>(this ApiResult<T> apiResult)
    {
        return apiResult.Status;
    }

    #endregion 拓展方法
}

public class TaskOrderUploadImageResult
{
    public string ImageUrl { get; set; } = string.Empty;
}